package easy.移除元素;

import java.util.Arrays;

/**
 * 移除元素
 *
 * @author CodeX
 * @since 2021-09-28 09:29:30
 */
public class RemoveElement {

    public int removeElement(int[] nums, int val) {
        int length = nums.length;
        if (length == 0) {
            return 0;
        }

        int index = 0;
        while (index < length) {
            if (nums[index] == val) {
                nums[index] = nums[length - 1];
                length--;
            } else {
                index++;
            }
        }
        return length;
    }

    public static void main(String[] args) {
        RemoveElement re = new RemoveElement();
        int[] nums = {0,1,2,2,3,0,4,2};
        int i = re.removeElement(nums, 2);
        System.out.println(i);
        System.out.println(Arrays.toString(nums));
    }
}
